package com.cares.event.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Element;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cares.event.constant.EventDefinitionConstant;
import com.cares.event.constant.EventFlowConstant;
import com.cares.event.constant.EventSystemConstant;
import com.cares.event.constant.XmlNodeConstant;
import com.cares.event.dao.EventExecutionDao;
import com.cares.event.dao.EventProcessDao;
import com.cares.event.dto.EventActivityDto;
import com.cares.event.dto.EventExecutionDto;
import com.cares.event.dto.EventProcessDto;
import com.cares.event.entity.TEventExecution;
import com.cares.event.entity.TEventProcess;
import com.cares.event.service.EventExecutionService;
import com.cares.event.utils.EventFlowXmlUtils;

/**
 * 事件流service实现类
 * @author xuji
 *
 */
@Service("eventExecutionService")
public class EventExecutionServiceImpl implements EventExecutionService {
	
	@Autowired
	private EventExecutionDao eventExecutionDao;
	
	@Autowired
	private EventProcessDao eventProcessDao;
	
	/**
	 * 服务器启动时， 查找所有的事件流程执行信息
	 * @return
	 */
	public List<EventActivityDto> queryEventExecutionDtos(){
		List<TEventExecution> tEventExecutions = eventExecutionDao.queryAllEventExecutions();
		List<EventActivityDto> results = new ArrayList<EventActivityDto>();
		if(tEventExecutions.size()>0){
			List<TEventProcess> flowVOs = eventProcessDao.queryEventProcess(EventDefinitionConstant.EVENT_CLASS_EVENT,
																					EventDefinitionConstant.EVENT_ATTR_STATE_USING);
			//将流程模板与流程执行信息配对
			for(TEventExecution vo : tEventExecutions){
				EventActivityDto result = new EventActivityDto();
				BeanUtils.copyProperties(vo, result);
				//设置当前任务状态为：未执行
				result.setTaskStatus(EventFlowConstant.EVENT_FLOW_STATE_NO);
				//填充流程信息
				for(TEventProcess flowVO : flowVOs){
					if(vo.getEventFlowId()==flowVO.getEventFlowId()){
						result.setEventName(flowVO.getEventName());
						result.setEventVarsion(flowVO.getEventVarsion());
						result.setLobId(flowVO.getLobId());//是否立即启动
						result.setStartNow(flowVO.getStartNow());//启动时间
						result.setStartTime(flowVO.getStartTime());//是否循环执行
						result.setLoopRun(flowVO.getLoopRun());//循环间隔时间
						result.setLoopSpaceTime(flowVO.getLoopSpaceTime());
					}
				}
				results.add(result);
			}
		}
		return results;
	}
	
	

	//启动流程
	@Override
	public boolean startEventFlow(EventProcessDto jsflowProcessDto) {
		
		TEventProcess tEventProcess = eventProcessDao.getEventProcess(jsflowProcessDto.getEventFlowId());
		//事件流xml文本
		Element firtTask = EventFlowXmlUtils.getStartTask(tEventProcess.getTEventLob().getEventXml());
		
		//解析第一个事件任务信息，创建流程执行实例JsflowExecutionVO
		TEventExecution jsflowExecutionVO = new TEventExecution();
		
		jsflowExecutionVO.setEventFlowId(tEventProcess.getEventFlowId());
		//存储任务节点唯一属性name的值
		jsflowExecutionVO.setActivityName(firtTask.attributeValue(XmlNodeConstant.NODE_ATTR_NAME));
		jsflowExecutionVO.setActivityType(firtTask.getName()); 
		jsflowExecutionVO.setTaskStatus(EventFlowConstant.EVENT_TASK_STATE_NO);
		eventExecutionDao.saveEventExecution(jsflowExecutionVO);
		
		//分析当前事件任务，将要执行的任务存入任务列队中
		EventActivityDto eventActivityDto = new EventActivityDto();
		BeanUtils.copyProperties(jsflowExecutionVO,eventActivityDto);
		//流程名称
		eventActivityDto.setEventName(tEventProcess.getEventName());
		//版本号
		eventActivityDto.setEventVarsion(tEventProcess.getEventVarsion());
		//是否立即启动
		eventActivityDto.setStartNow(tEventProcess.getStartNow());
		//启动时间
		eventActivityDto.setStartTime(tEventProcess.getStartTime());
		//是否循环执行
		eventActivityDto.setLoopRun(tEventProcess.getLoopRun());
		//循环间隔时间
		eventActivityDto.setLoopSpaceTime(tEventProcess.getLoopSpaceTime());
		//xml文件信息
		eventActivityDto.setLobId(tEventProcess.getTEventLob().getLobId());
		eventActivityDto.setEventXml(tEventProcess.getTEventLob().getEventXml());
		return EventSystemConstant.addJsflowExecutionDto(eventActivityDto);
	}
	
	/**
	 * 根据流程执行id删除执行表
	 * @param executionId
	 */
	public void deleteEventExecution(long executionId){
		eventExecutionDao.deleteEventExecution(executionId);
	}
	
	
	/**
	 * 查询流程实例运行
	 * @param jsflowProcessVO
	 */
	public TEventExecution getEventExecution(long executionId){
		return eventExecutionDao.getEventExecution(executionId);
	}

	/**
	 * 保存流程实例运行
	 * @param jsflowProcessVO
	 */
	public void doSaveEventExecution(EventExecutionDto eventExecutionDto){
		TEventExecution tEventExecution = new TEventExecution();
		BeanUtils.copyProperties(eventExecutionDto, tEventExecution);
		eventExecutionDao.saveEventExecution(tEventExecution);
	}
	
	/**
	 * 更新流程实例运行
	 * @param jsflowProcessVO
	 */
	public void doUpdateEventExecution(EventExecutionDto eventExecutionDto){
		TEventExecution tEventExecution = eventExecutionDao.getEventExecution(eventExecutionDto.getExecutionId());
		if(StringUtils.isNotBlank(eventExecutionDto.getActivityAttribute())){
			tEventExecution.setActivityAttribute(eventExecutionDto.getActivityAttribute());
		}
		if(StringUtils.isNotBlank(eventExecutionDto.getActivityName())){
			tEventExecution.setActivityName(eventExecutionDto.getActivityName());
		}
		if(StringUtils.isNotBlank(eventExecutionDto.getActivityType())){
			tEventExecution.setActivityType(eventExecutionDto.getActivityType());
		}
		if(StringUtils.isNotBlank(eventExecutionDto.getEventClass())){
			tEventExecution.setEventClass(eventExecutionDto.getEventClass());
		}
		if(null != eventExecutionDto.getEventFlowId()){
			tEventExecution.setEventFlowId(eventExecutionDto.getEventFlowId());
		}
		if(StringUtils.isNotBlank(eventExecutionDto.getLastResult())){
			tEventExecution.setLastResult(eventExecutionDto.getLastResult());
		}
		if(StringUtils.isNotBlank(eventExecutionDto.getTaskStatus())){
			tEventExecution.setTaskStatus(eventExecutionDto.getTaskStatus());
		}
		eventExecutionDao.updateEventExecution(tEventExecution);
	}
	
	/**
	 *查找指定事件流的执行任务
	 * @param jsflowProcessVO
	 */
	public List<TEventExecution> queryEventExecution(EventExecutionDto jsflowExecutionDto){
		return eventExecutionDao.queryEventExecutions(jsflowExecutionDto);
	}

}
